<template>
  <div id="app">
    <h1>{{title}}</h1>
    <div class="loader" v-if="!heroes"></div>
    <ul>
      <li class="hero" v-for="hero in heroes" v-on:click="onSelect(hero)" v-bind:class="{'select':selectObj===hero}">
        id:{{hero.id}}
        name:{{hero.name}}
      </li>
      <li class="hero" v-on:click="onAdd=true;selectObj=null;" v-bind:class="{'select':onAdd}">
        <add-component v-bind:heroes="heroes"></add-component>
      </li>
    </ul>
    <about-component v-bind:hero="selectObj"></about-component>
    <div class="json">
      <h2>All JSON string:</h2>
      <p>{{heroes}}</p>
    </div>
  </div>
</template>

<script>
  import Test from './test.vue'
  import About from './about.vue'
  import AddHero from './addHero'


  export default {
    name: 'app',
    data() {
      return {
        title: 'The Tour of Heroes!',
        heroes: null,
        selectObj: null,
        onAdd: null,
      }
    },
    methods: {
      onSelect(obj) {
        console.log('select!');
        this.selectObj = obj;
        this.onAdd = null;
      },
      getHeroes() {
        fetch("hero/HEROES.json").then(res => {
            if (res.ok) res.json().then(data => setTimeout(() => this.heroes = data, 1000));
            else console.log("Looks like the response wasn't perfect, got status", res.status)
          }, e => console.log(e)
        )
      },
      onDelete() {
        console.log('delete!')
      }
    },
    components: {
      'test-component': Test, 'about-component': About, 'add-component': AddHero
    },
    created: function () {
      this.getHeroes();
      document.onkeydown = function () {
        this.onDelete();
      }
    }

  }
</script>

<style>
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
  }

  .json {
    margin-top: 2em;
    margin-left: 20%;
    margin-right: 20%;
    border-top: 1px solid #42b983;
    padding-top: 3em;
  }

  .select {
    color: #42b983;
    border: 1px solid #42b983;
  }

  .hero {
    padding: 2em;
    margin-bottom: 1em;
  }

  .hero:hover {
    border: 1px solid #42b983;
  }

  h1, h2 {
    font-weight: normal;
  }

  ul {
    list-style-type: none;
    padding: 0;
  }

  li {
    display: inline-block;
    margin: 0 10px;
  }

  a {
    color: #42b983;
  }
</style>
